import { getToken, removeToken, setToken } from '@/utils/auth'
import { login, getInfo } from '@/api/user'
export default {
  namespaced: true,
  state() {
    return {
      token: getToken(),
      userinfo: {}
    }
  },
  mutations: {
    updateToken(state, payload) {
      state.token = payload
      console.log(payload)
      setToken(payload)
    },
    delToken(state) {
      state.token = null
      removeToken()
    },
    // 更新用户信息方法
    updateUserInfo(state, payload) {
      state.userinfo = payload
    }

  },
  actions: {
    async storeLogin(context, payload) {
      const data = await login(payload)
      context.commit('updateToken', data)
    },
    async storeGetUserInfo(context) {
      const data = await getInfo()
      console.log(data)
      context.commit('updateUserInfo', data)
    },
    // 发起登出的方法
    storeGet(context) {
      context.commit('delToken')
      context.commit('updateUserInfo', {})
    }
  }

}
